package com.wk.springboot.config;

import com.alibaba.druid.pool.DruidDataSource;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@MapperScan(basePackages = {"com.thunisoft.qbmh.mapper.bzk"}, sqlSessionTemplateRef = "BzkSqlSessionTemplate")
public class MybatisBzkConfig {


    @Bean(name = "BzkDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.bzk")
    public DataSource primaryDataSource() {
        return new DruidDataSource();
    }

    @Bean(name = "BzkSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory1(@Qualifier("BzkDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        return factoryBean.getObject();
    }

    @Bean(name = "BzkSqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate1(@Qualifier("BzkSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
